package cn.edu.sgu.www.mhxysy.service.equipment.impl;

import cn.edu.sgu.www.mhxysy.vo.Imager;
import cn.edu.sgu.www.mhxysy.entity.equipment.EquipmentCategory;
import cn.edu.sgu.www.mhxysy.entity.equipment.EquipmentType;
import cn.edu.sgu.www.mhxysy.mapper.equipment.EquipmentCategoryMapper;
import cn.edu.sgu.www.mhxysy.mapper.equipment.EquipmentTypeMapper;
import cn.edu.sgu.www.mhxysy.service.equipment.EquipmentTypeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.function.Consumer;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class EquipmentTypeServiceImpl implements EquipmentTypeService {

	private final EquipmentTypeMapper equipmentTypeMapper;
	private final EquipmentCategoryMapper equipmentCategoryMapper;

	@Autowired
	public EquipmentTypeServiceImpl(
			EquipmentTypeMapper equipmentTypeMapper,
			EquipmentCategoryMapper equipmentCategoryMapper) {
		this.equipmentTypeMapper = equipmentTypeMapper;
		this.equipmentCategoryMapper = equipmentCategoryMapper;
	}

	@Override
	public List<Imager> selectAll() {
		List<Imager> list = equipmentTypeMapper.selectAll();

		for (Imager imager : list) {
			QueryWrapper<EquipmentCategory> queryWrapper = new QueryWrapper<>();

			queryWrapper.eq("type", imager.getId());
			queryWrapper.eq("grade", 100);

			EquipmentCategory equipmentCategory = equipmentCategoryMapper.selectOne(queryWrapper);

			if (equipmentCategory != null) {
				imager.setImage(equipmentCategory.getImage());
			}
		}

		return list;
	}

	@Override
	public List<Imager> selectTypes() {
		List<Imager> list = equipmentTypeMapper.selectTypes();

		list.forEach(new Consumer<Imager>() {
			@Override
			public void accept(Imager imager) {
				String image = equipmentCategoryMapper.selectImage(Integer.parseInt(imager.getId()));

				imager.setImage(image);
			}
		});

		return list;
	}

	@Override
	public List<Imager> selectParent() {
		return equipmentTypeMapper.selectParent();
	}

	@Override
	public Imager selectTop(Integer id) {
		return equipmentTypeMapper.selectTop(id);
	}

	@Override
	public List<Imager> selectWeaponry() {
		return equipmentTypeMapper.selectWeaponry();
	}

	@Override
	public Imager selectType(Integer type) {
		EquipmentType equipmentType = equipmentTypeMapper.selectById(type);
		Imager imager = new Imager();

		imager.setId(equipmentType.getId().toString());
		imager.setName(equipmentType.getName());
		imager.setImage(equipmentCategoryMapper.selectImage(type));

		return imager;
	}

	@Override
	public EquipmentType selectById(Integer id) {
		return equipmentTypeMapper.selectById(id);
	}

}